package ch.zbw.pra.sysventory.ui.gui.utilities;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.text.JTextComponent;

public class GuiLayout {
	// create a static gridBagLayout, who can used from everywhere
	public static GridBagLayout getGridBagLayout(final JComponent comp) {
		final GridBagLayout gridBagLayout = new GridBagLayout();
		comp.setLayout(gridBagLayout);
		return gridBagLayout;
	}

	/**
	 * anchor WEST fill HORIZONTAL insets ( 0, 0, 0, 0 )
	 * 
	 * for every gridBagLayout will also be needed a constraint, for give the
	 * behavior
	 * 
	 * @return GridBagConstatins
	 */
	public static GridBagConstraints getGridBagConstraints() {
		final GridBagConstraints gridBagConstraints = new GridBagConstraints();
		// the line starts on the left side and expand it's self on right side
		gridBagConstraints.anchor = GridBagConstraints.WEST;
		// the component will expand it's self on horizontal way
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		// defines the inner padding (top, left, bottom, right)
		gridBagConstraints.insets = new Insets(0, 0, 0, 0);
		// lets go to your method
		return gridBagConstraints;
	}

	// starts a new row in layouttable and set two components into the row
	public static void addNextRowToLayout(final JComponent container, final GridBagLayout gridBagLayout, final GridBagConstraints gridBagConstraints, int x, final int y, final JComponent toAddFirst,
			final JComponent toAddSecond) {
		GuiLayout.addToLayout(container, gridBagLayout, gridBagConstraints, x, y, toAddFirst);
		GuiLayout.addToLayout(container, gridBagLayout, gridBagConstraints, ++x, y, GridBagConstraints.REMAINDER, gridBagConstraints.gridheight, 1.0, 0.0, toAddSecond);
	}

	// adds a component in line of the container
	public static void addToLayout(final JComponent container, final GridBagLayout gridBagLayout, final GridBagConstraints gridBagConstraints, final int x, final int y, final JComponent toAdd) {
		GuiLayout.addToLayout(container, gridBagLayout, gridBagConstraints, x, y, gridBagConstraints.gridwidth, gridBagConstraints.gridheight, toAdd);
	}

	// adds a component in line with width and height rest auto
	public static void addToLayout(final JComponent container, final GridBagLayout gridBagLayout, final GridBagConstraints gridBagConstraints, final int x, final int y, final int gridwidth, final int gridheight,
			final JComponent toAdd) {
		GuiLayout.addToLayout(container, gridBagLayout, gridBagConstraints, x, y, gridwidth, gridheight, gridBagConstraints.weightx, gridBagConstraints.weighty, gridBagConstraints.ipadx, gridBagConstraints.ipady, toAdd);
	}

	// adds a component in line with width and height and fix it in x and y
	// position rest auto
	public static void addToLayout(final JComponent container, final GridBagLayout gridBagLayout, final GridBagConstraints gridBagConstraints, final int x, final int y, final int gridwidth, final int gridheight,
			final double weightx, final double weighty, final JComponent toAdd) {
		GuiLayout.addToLayout(container, gridBagLayout, gridBagConstraints, x, y, gridwidth, gridheight, weightx, weighty, gridBagConstraints.ipadx, gridBagConstraints.ipady, toAdd);
	}

	// adds a component in line with width and height and fix it in x and y
	// position, and defined margin intern and extern, nothing is auto
	public static void addToLayout(final JComponent container, final GridBagLayout gridBagLayout, final GridBagConstraints gridBagConstraints, final int x, final int y, final int gridwidth, final int gridheight,
			final double weightx, final double weighty, final int ipadx, final int ipady, final JComponent toAdd) {
		gridBagConstraints.gridx = x;
		gridBagConstraints.gridy = y;
		gridBagConstraints.weightx = 0.0;
		gridBagConstraints.weighty = 0.0;

		gridBagConstraints.gridwidth = gridwidth;
		gridBagConstraints.gridheight = gridheight;
		gridBagConstraints.ipadx = ipadx;
		gridBagConstraints.ipady = ipady;

		gridBagLayout.setConstraints(toAdd, gridBagConstraints);
		container.add(toAdd);
	}

	// create funky line around a layout
	public static Border createMyTitledBorder(final String name) {
		return BorderFactory.createTitledBorder(name);
	}

	// different the backgroundcolor in green, if it isn't chanching anything,
	// if change something, then in red
	public static void changeBackgroundIfAnyChanges(JTextField textfield, String old, String current) {
		if (old.isEmpty() && !current.isEmpty()) // A new value was added
		{
			textfield.setBackground(Color.GREEN);
		} else if (!old.equals(current)) // An existing value has changed
		{
			textfield.setBackground(Color.RED);
		}

		if (old.equals(current)) {
			textfield.setBackground(new JTextField().getBackground());
		}
	}

	public static void changeBackgroundIfAccessDenied(JTextComponent jTextArea, String message, JComponent jComponentTextField) {

		jComponentTextField.setBackground(Color.red);
		jTextArea.setText(message);
		jTextArea.setBackground(Color.red);

	}

	// take the position of the window in center of screen
	public static void centerWindow(Window window) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension windowSize = window.getPreferredSize();
		if (windowSize.height > screenSize.height) {
			windowSize.height = screenSize.height;
		}
		if (windowSize.width > screenSize.width) {
			windowSize.width = screenSize.width;
		}

		int positionX = (screenSize.width - windowSize.width) / 2;
		int positionY = (screenSize.height - windowSize.height) / 2;

		// if (SysventoryApp.IS_WINDOWS) {
		// positionX -= (windowSize.width / 2);
		// positionY -= (windowSize.height / 2);
		// }
		window.setLocation(positionX, positionY);
	}
}
